package com.crm.controller;

import com.crm.common.result.AjaxResult;
import com.crm.common.result.PageResult;
import com.crm.common.utils.UserThreadLocal;
import com.crm.domain.Clue;
import com.crm.dto.CluePageQueryDto;
import com.crm.service.ClueService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/clues/clue")
public class ClueController {

    private final ClueService clueService;

    @GetMapping("/list")
    public PageResult page(CluePageQueryDto dto){
        PageResult pageResult = clueService.pageClue(dto);
        return pageResult;
    }

    @PostMapping
    public AjaxResult insertClue(@RequestBody Clue clue){
        Long userId = UserThreadLocal.getUserId();
        //todo 根据userid查询user,将名字设置给clue的owner
        clue.setOwner("admin");
        clue.setUserId(userId);
        clue.setOwnerTime(LocalDateTime.now());
        clue.setCreateTime(LocalDateTime.now());
        clue.setStatus(Clue.CLUE_STATUS_WAITING);
        clue.setLevel(Clue.CLUE_LEVEL_WATCHING);
        clueService.save(clue);
        return AjaxResult.success();
    }

    @GetMapping("/{id}")
    public AjaxResult getClueById(@PathVariable Long id){
        return AjaxResult.success(clueService.getById(id));
    }

    @PutMapping
    public AjaxResult updateClue(@RequestBody Clue clue){
        clueService.updateById(clue);
        return AjaxResult.success();
    }

    @GetMapping("/pool")
    public AjaxResult pageCluePool(CluePageQueryDto dto){
        return clueService.pageCluePool(dto);
    }

    @PutMapping("/gain")
    public AjaxResult gainClue(List<Long> ids,Long userId){
        return AjaxResult.success();
    }

}
